package hello;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;

public class SimpleJGroupsApp extends ReceiverAdapter {
    private JChannel channel;

    public static void main(String[] args) throws Exception {
        new SimpleJGroupsApp().start();
    }

    private void start() throws Exception {
        channel = new JChannel("jgroups.xml");
        channel.setReceiver(this);
        channel.connect("MyCluster");
        channel.getState(null, 10000);

        String message = "Hello, JGroups!";
        channel.send(new Message(null, message));

        System.out.println("Press enter to exit...");
        System.in.read();
        channel.close();
    }

    @Override
    public void receive(Message msg) {
        String message = msg.getObject();
        System.out.println("Received message: " + message);
    }

    @Override
    public void viewAccepted(View newView) {
        System.out.println("View changed: " + newView);
    }

}
